271. IOT設備裝Prometheus搞Remote write

WHY

監控IOT設備的一些基礎效能,
node_exporter裝好了,
但是怎麼把metrics丟出去..??

SOLUTION

前置作業

執行node_exporter

讓node_exporter背景執行。

nohup ./node_exporter > node_exporter.log 2>&1 &

Prometheus Server設定

需開啟 remote-write-receiver
在參數的地方加上 --web.enable-remote-write-receiver
然後由於要對外,看你是使用哪種方式,就設定對外。
記得安全要做好,看是檔IP或是要用基本驗證

- name: prometheus-server
  image: "prom/prometheus:v2.48.0"
  imagePullPolicy: "IfNotPresent"
  args:
    - --web.enable-remote-write-receiver
    - --storage.tsdb.retention.time=95d
    - --config.file=/etc/config/prometheus.yml
    - --storage.tsdb.path=/data
    - --web.console.libraries=/etc/prometheus/console_libraries
    - --web.console.templates=/etc/prometheus/consoles
    - --web.enable-lifecycle

IOT設備

Step 1. 安裝Prometheus

由於這個IOT裝置,本身是busybox,很多東西不能用,
(apt,yum,apk這些通通不支援)
所以是先把壓縮包放到ftp,
(下載時建議先用uname -a檢查一下OS版本,我這都是裝arm的版本)
再用sftp的指令下載。

sftp -P 222  abc@123.123.13.13
get prometheus.tar.gz
tar -zxvf prometheus.tar.gz

Step 2. remote write設定檔

global:
  scrape_interval: 15s  # 每 15 秒抓取一次數據
  evaluation_interval: 15s  # 每 15 秒執行規則評估

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']

remote_write:
  - url: "https://remotewrite.test.com/api/v1/write"
    remote_timeout: 30s
    queue_config:
      batch_send_deadline: 5s
      max_samples_per_send: 100
      max_shards: 1
    write_relabel_configs:
      - source_labels: [__name__]
        regex: ".*"
        action: replace
        target_label: "iot"
        replacement: "shining_iot"

ref. Remote_write config

Step 3. 執行Prometheus

./prometheus --config.file ../prometheus.yml
Step 3-1. 除錯

然後,Prometheus Client 就出錯了。

tls: failed to verify certificate: x509: certificate signed by unknown authority

看錯誤是憑證錯誤,所以先用curl驗證一下,
這台機器也沒辦法裝curl,所以還是只能去下載curl到ftp後,
再去ftp下載。

curl -v 在IOT設備,驗證promtheus server後出現錯誤

curl -v https://remotewrite.test.com/api/v1/write

error setting certificate file: /etc/ssl/certs/ca-certificates.crt

這樣看起來應該都是IOT設備沒有裝憑證導致。
透過curl下載letsencrypt憑證

curl -o /etc/ssl/certs/ISRG_Root_X1.pem https://letsencrypt.org/certs/isrgrootx1.pem

但是,上面有提到curl -v 驗證https會發生錯誤,
所以,無法下載。
此時,回歸老方法,下載後丟去ftp,在傳到IOT設備。

下載後,根據上面的錯誤訊息,建立資料夾
isrgrootx1.pem 改名成 ca-certificates.crt放到 /etc/ssl/certs

mkdir -p /etc/ssl/certs
cp isrgrootx1.pem /etc/ssl/certs/ca-certificates.crt

再驗證一次 ,正常。

curl -v https://remotewrite.test.com/api/v1/write

Step 4. 再度執行Prometheus

這次執行後,出現另一個錯誤

level=ERROR source=db.go:1109 msg="compaction failed" component=tsdb err="preallocate: no space left on device"

檢查硬碟空間與inode都有足夠的空間,
發生這種情況是由於Prometheus的WAL(預寫日誌)的機制,
為了保證數據可靠性,所以會根據公式推出需要儲存的容量。

needed_disk_space = retention_time_seconds * ingested_samples_per_second * bytes_per_sample

ref.Prometheus Storage

但IOT設備空間本來就小,
所以只好縮!!!

./prometheus --storage.tsdb.retention.time=10m --storage.tsdb.retention.size=1MB --config.file ../prometheus.yml

最後到Prometheus Server看看有沒有資料傳上去就好了。
傳不上去的話就是看上傳的地方是否有地方要更改。

題外話

看到有人說,開啟Remote Write的話,
會導致Server記憶體使用量上升。
目前還沒大量使用,也只能等到時再看看。
ref. Prometheus Remote Write Performance Issue